<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    :width="700"
    destroy-on-close
    append-to-body
    @closed="emits('closed')"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="dialogForm"
      label-width="80px"
      :style="'el-dialog' === 'el-drawer' ? 'padding:0 20px' : ''"
    >

      <el-form-item label="客户" prop="cus_code">
        <el-select v-model="form.cus_code" style="width:100%" clearable placeholder="请选择客户">

          <el-option
            v-for="(item, index) in dictData.wmsMdCusList"
            :key="index" :label="item.zhong_wen_qch"
            :value="item.id"
          >{{ item.zhong_wen_qch }}</el-option>

        </el-select>
      </el-form-item>

      <el-form-item label="费用" prop="coswms_t_code">
        <el-select v-model="form.coswms_t_code" style="width:100%" clearable placeholder="请选择费用">

          <el-option
            v-for="(item, index) in dictData.wms_billing_fee"
            :key="index" :label="item.label"
            :value="item.value"
          >{{ item.label }}</el-option>

        </el-select>
      </el-form-item>

      <el-form-item label="费用名称" prop="coswms_t_name">
        <el-input v-model="form.coswms_t_name" clearable placeholder="请输入费用名称" />
      </el-form-item>

      <el-form-item label="费用日期" prop="coswms_t_data">
        <el-date-picker
          v-model="form.coswms_t_data" placeholder="请选择费用日期"
          type="datetime"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>

      <el-form-item label="每日费用" prop="day_coswms_t_yj">
        <el-input-number v-model="form.day_coswms_t_yj" controls-position="right" />
      </el-form-item>

      <el-form-item label="不含税价" prop="day_coswms_t_bhs">
        <el-input v-model="form.day_coswms_t_bhs" clearable placeholder="请输入不含税价" />
      </el-form-item>

      <el-form-item label="税额" prop="day_coswms_t_se">
        <el-input v-model="form.day_coswms_t_se" clearable placeholder="请输入税额" />
      </el-form-item>

      <el-form-item label="含税价" prop="day_coswms_t_hsj">
        <el-input v-model="form.day_coswms_t_hsj" clearable placeholder="请输入含税价" />
      </el-form-item>

      <el-form-item label="费用来源" prop="coswms_t_ori">
        <el-input v-model="form.coswms_t_ori" clearable placeholder="请输入费用来源" />
      </el-form-item>

      <el-form-item label="备注" prop="beizhu">
        <el-input v-model="form.beizhu" clearable placeholder="请输入备注" />
      </el-form-item>

      <el-form-item label="计费数量" prop="coswms_t_sl">
        <el-input v-model="form.coswms_t_sl" clearable placeholder="请输入计费数量" />
      </el-form-item>

      <el-form-item label="数量单位" prop="coswms_t_unit">
        <el-input v-model="form.coswms_t_unit" clearable placeholder="请输入数量单位" />
      </el-form-item>

      <el-form-item label="是否已结算" prop="coswms_t_js">
        <el-switch v-model="form.coswms_t_js" :active-value="proxy.$CONFIG.SWITCH.ENABLE" :inactive-value="proxy.$CONFIG.SWITCH.DISABLE" />
      </el-form-item>

			<el-form-item label="状态" prop="status">
				<el-switch v-model="form.status" :active-value="proxy.$CONFIG.SWITCH.ENABLE" :inactive-value="proxy.$CONFIG.SWITCH.DISABLE" clearable placeholder="请输入状态"/>
			</el-form-item>

      <el-form-item label="排序" prop="sort">
        <el-input v-model="form.sort" clearable placeholder="请输入排序" />
      </el-form-item>

    </el-form>
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
  import { ref, reactive, defineExpose, onMounted,getCurrentInstance } from 'vue'
  import {useRouter} from "vue-router";
  import { ElMessage } from 'element-plus'
  import wmsWmDayCost from '@/api/apis/wms/wmsWmDayCost'
	import wmsMdCus from '@/api/apis/wms/wmsMdCus'
	import systemDict from '@/api/apis/system/dataDict'


  const emits = defineEmits(['success', 'closed'])
  const { proxy } = getCurrentInstance();
  const router = useRouter();

  const mode = ref('add')
  const treeList = ref([])
  const visible = ref(false)
  const isSaveing = ref(false)
  const dialogForm = ref(null)

  const titleMap = reactive({ add: '新增计费', edit: '编辑计费' })
  const dictData = reactive({
    sex: [],
    wms_billing_fee: [],
    platform_data_status: [],
		wmsMdCusList:[],

	})
  const form = reactive({
    id:'',
cus_code:'',
coswms_t_code:'1',
coswms_t_name:'',
coswms_t_data:null,
day_coswms_t_yj:0,
day_coswms_t_bhs:'',
day_coswms_t_se:'',
day_coswms_t_hsj:'',
coswms_t_ori:'',
beizhu:'',
coswms_t_sl:'',
coswms_t_unit:'',
coswms_t_js:proxy.$CONFIG.SWITCH.ENABLE,
status:proxy.$CONFIG.SWITCH.ENABLE,
sort:'0',
mer_id:'0',

  })
  const rules = reactive({
    cus_code: [{required: true, message: '客户必填', trigger: 'blur' }],
    coswms_t_code: [{required: true, message: '费用必填', trigger: 'blur' }],
    coswms_t_name: [{required: true, message: '费用名称必填', trigger: 'blur' }],

  })

  onMounted(async () => {
    await getDictData()
    if(router.currentRoute.value.query.open) {
    	open('add');
    }
  })

  const getDictData = () => {
    systemDict.getDict('sex').then(res => {
      dictData.sex = res.data
    })
    systemDict.getDict('wms_billing_fee').then(res => {
      dictData.wms_billing_fee = res.data
    })
    systemDict.getDict('platform_data_status').then(res => {
      dictData.platform_data_status = res.data
    })
		// 客户列表
		wmsMdCus.list().then(res => {
			dictData.wmsMdCusList = res.data
		})

  }

  const open = (type = 'add') => {
    mode.value = type
    visible.value = true

  }

  const submit = () => {
    dialogForm.value.validate(async (valid) => {
      if (valid) {
        isSaveing.value = true
        const res = mode.value === 'add' ? await wmsWmDayCost.save(form) : await wmsWmDayCost.update(form.id, form)
        if (res.success) {
          emits('success', form, mode.value)
          visible.value = false
          ElMessage.success(res.message)
        } else {
          ElMessage.error(res.message)
        }
        isSaveing.value = false
      }
    })
  }

  const setData = (data) => {
    for (let k in form) {
      if (data[k] || data[k] === 0) {
        form[k] = data[k]
      }
    }
  }


  defineExpose({
    open, setData
  })
</script>
